package me.zhanshi123.lab4.dao;

import me.zhanshi123.lab4.entity.CategoryStatistics;
import me.zhanshi123.lab4.entity.Good;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Mapper
public interface GoodDao {
    @Select("SELECT `id`,`name`,`type`,`origin`,`price`,`production`,`manufacturer` FROM `goods_item` LIMIT ${start},${span}")
    List<Good> getAllGoods(@Param(value = "start") int start, @Param(value = "span") int span);

    @Select("<script>" +
            "SELECT `id`,`name`,`type`,`origin`,`price`,`production`,`manufacturer` FROM `goods_item` " +
            "<where>" +
            "<if test='id != -1'>" +
            "`id` = #{id}" +
            "</if>" +
            "<if test='type != null'>" +
            "`type` = #{type}" +
            "</if>" +
            "<if test='name != null'>" +
            "`name` = #{name}" +
            "</if>" +
            "<if test='origin != null'>" +
            "`origin` = #{origin}" +
            "</if>" +
            "<if test='manufacturer != null'>" +
            "`manufacturer` = #{manufacturer}" +
            "</if>" +
            "</where>" +
            "LIMIT ${start},${span}" +
            "</script>")
    List<Good> getGoodsBy(@Param(value = "start") int start, @Param(value = "span") int span, @Param(value = "id") long id, @Param(value = "name") String name
            , @Param(value = "type") String type, @Param(value = "origin") String origin, @Param(value = "manufacturer") String manufacturer);

    @Select("<script>" +
            "SELECT COUNT(`id`) FROM `goods_item` " +
            "<where>" +
            "<if test='id != -1'>" +
            "`id` = #{id}" +
            "</if>" +
            "<if test='type != null'>" +
            "`type` = #{type}" +
            "</if>" +
            "<if test='name != null'>" +
            "`name` = #{name}" +
            "</if>" +
            "<if test='origin != null'>" +
            "`origin` = #{origin}" +
            "</if>" +
            "<if test='manufacturer != null'>" +
            "`manufacturer` = #{manufacturer}" +
            "</if>" +
            "</where>" +
            "" +
            "</script>")
    int getGoodsCount(@Param(value = "id") long id, @Param(value = "name") String name
            , @Param(value = "type") String type, @Param(value = "origin") String origin, @Param(value = "manufacturer") String manufacturer);

    @Update("INSERT INTO `goods_item`(`id`,`name`,`type`,`origin`,`price`,`production`,`manufacturer`)" +
            " VALUES(#{id},#{name},#{type},#{origin},#{price},#{production},#{manufacturer}) ON DUPLICATE KEY UPDATE " +
            "`name` = #{name}, `type`=#{type}, `origin`=#{origin}, `price`=#{price},`production`=#{production},`manufacturer`=#{manufacturer}")
    int updateGood(@Param(value = "id") long id, @Param(value = "name") String name, @Param(value = "type") String type, @Param(value = "origin") String origin,
                   @Param(value = "price") double price, @Param(value = "production") Date date, @Param(value = "manufacturer") String manufacturer);

    @Update("INSERT INTO `goods_item`(`name`,`type`,`origin`,`price`,`production`,`manufacturer`)" +
            " VALUES(#{name},#{type},#{origin},#{price},#{production},#{manufacturer})")
    int addGood(@Param(value = "name") String name, @Param(value = "type") String type, @Param(value = "origin") String origin,
                @Param(value = "price") double price, @Param(value = "production") Date date, @Param(value = "manufacturer") String manufacturer);

    @Delete("DELETE FROM `goods_item` WHERE `id`=#{id}")
    int removeGood(@Param(value = "id") long id);

    List<CategoryStatistics> getCategoryStatistics();
}
